<template>
  <div>
    <!--    <van-tabbar route v-model="active">-->
    <van-tabbar route v-model="active" @change="onChange">
      <van-tabbar-item replace icon="wap-home">首页</van-tabbar-item>
      <van-tabbar-item replace icon="description-o">订单</van-tabbar-item>
      <van-tabbar-item replace icon="credit-pay">钱包</van-tabbar-item>
      <van-tabbar-item replace icon="chat-o">消息</van-tabbar-item>
      <van-tabbar-item replace icon="manager">我的</van-tabbar-item>
      <van-tabbar-item replace icon="search">AI询问</van-tabbar-item>
    </van-tabbar>
  </div>
</template>

<script>
export default {
  data() {
    return {
      active: 0,
    };
  },
  methods: {
    onChange(index) {
      // 更新 active 索引
      this.active = index;
      // 定义基于索引的路由
      const routes = [
        '/',                // 索引 0: 首页
        '/orderList',       // 索引 1: 订单
        '/purse_home',      // 索引 2: 钱包
        '/informationList', // 索引 3: 消息
        '/personalHome',     // 索引 4: 我的
        '/aiHome'          // 索引 5: AI询问
      ];
      // 获取当前路由
      const currentRoute = this.$route.path;
      // 检查用户是否已登录
      if (window.localStorage.getItem('userId')) {
        // 检查新路由是否与当前路由不同
        const newRoute = routes[index];
        if (currentRoute !== newRoute) {
          this.$router.push(newRoute);
        }
      } else {
        // 如果用户未登录，则导航到登录页
        if (currentRoute !== '/login') {
          this.$router.push('/login');
        }
      }
    }
  },
};
</script>
<style>

</style>
